package com.example.mqtt.controller;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.mqtt.cache.RowingCache;
import com.example.mqtt.entity.Rowing;
import com.example.mqtt.mapper.RowingMapper;
import com.example.mqtt.service.Imp.RowingServiceImp;
import com.example.mqtt.service.RowingService;
import com.example.mqtt.thread.SchedulePool;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/*
 *
 * 主要是训练开始的流程接口
 * */
@RestController
@Api(tags = "训练")
@RequestMapping("/train")
@Slf4j
public class TrainController {

    private final SchedulePool schedulePool;

    private final RowingMapper rowingMapper;

    private final RowingService rowingService;

    private final RowingServiceImp rowingServiceImp;

    @Autowired
    public TrainController(SchedulePool schedulePool, RowingMapper rowingMapper, RowingService rowingService, RowingServiceImp rowingServiceImp) {
        this.schedulePool = schedulePool;
        this.rowingMapper = rowingMapper;
        this.rowingService = rowingService;
        this.rowingServiceImp = rowingServiceImp;
    }

    @ApiOperation("开始训练")
    @PostMapping("/startFateDataBySelect")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "rowingId", required = true),
            @ApiImplicitParam(name = "isRealData", dataType = "boolean", required = true),
            @ApiImplicitParam(name = "isEnv_stats_velocity_data", dataType = "boolean", required = true),
            @ApiImplicitParam(name = "isJointsData", dataType = "boolean", required = true),
    })
    public String startTrainingByBindingIdAndRowingId(String rowingId,boolean isRealData,boolean isEnv_stats_velocity_data,boolean isJointsData) {
        String msg = "successful";
        if (rowingService.getRowingById(rowingId) == null) {
            msg = "rowing is not exist";
            return msg;
        }
        if (!schedulePool.isIdleScheduleTask(rowingId)) {
            msg = "thread is running";
            return msg;
        }
        schedulePool.scheduleForUser(rowingId,isRealData,isEnv_stats_velocity_data,isJointsData);
        return msg;
    }


    @ApiOperation("结束训练")
    @GetMapping("/endTrainingByRowingId")//结束训练的标志
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "rowingId", required = true)
    })
    public String endTrainingByRowingId(String rowingId) {
        String msg = "successful";
        if (StringUtils.isBlank(rowingId)) {
            return "rowingId is null";
        }
        if (rowingService.getRowingById(rowingId) == null) {
            msg = "rowing is not exist";
            return msg;
        }
        if (schedulePool.isIdleScheduleTask(rowingId)) {
            msg = "thread is idle";
            return msg;
        }
        schedulePool.stopScheduleForUser(rowingId);
        RowingCache.remove(rowingId);
        return msg;
    }


    @ApiOperation("结束所有训练")
    @GetMapping("/endAllTraining")//结束训练的标志
    public String endAllTraining() {
        String msg = "successful";
        schedulePool.stopAllScheduleTask();
        return msg;
    }






}
